Ext.onReady(function(){
	
	Ext.QuickTips.init();

	// Disable browser right click
	Ext.fly(document.body).on('contextmenu', function(e, target) {
		e.preventDefault();
	});	
	var checklist = new GBIF.GeoSpatial.AvailableLayersTree({ 
					height: 400
				,	border:true
				,	title:"Available Layers"
			    ,   width: 400
				,	autoScroll:true
				,	renderTo: Ext.getBody()
				,	tbar:[{
		           			text: 'New Folder'
						//,	scope:this	
		            	,	handler: function(){
								/*
								 * this is replaced by checklist as it doesn't getting this here
								 * when using in main component relace checklist with this
								 */
								var root = checklist.getRootNode();
								if(checklist.getSelectionModel().selNode){
										root = checklist.getSelectionModel().selNode;
								}
								
		                		var node = root.appendChild(new Ext.tree.TreeNode({
				            	        	text:'Dummy'
		        	    	    	    ,	allowDrag:false
										,	leaf:false
										,	expanded:true
										,	checked:false
		                			}));
								 
								checklist.getSelectionModel().select(node);
		                		setTimeout(function(){
				                	    ge.editNode = node;
		        		            	ge.startEdit(node.ui.textNode);
		                			}, 10);
		            		}
		        	}]	
				,	listeners: {
						'contextmenu': function(node){
								var menu = new Ext.menu.Menu();
								menu.add(new Ext.menu.Item({
											text: 'Add Folder'
										,	handler: this.addLayerFolder.createDelegate(this, [node]) //character_add_attribute.createDelegate(this, [node])
								}));
								menu.add(new Ext.menu.Item({
											text: 'Rename Layer/Folder'
										,	handler: this.character_edit_attribute.createDelegate(this, [node])
								}));
								menu.add('-');
								menu.add(new Ext.menu.Item({
											text: 'Delete Layer/Folder'
										,	handler: this.character_delete_attribute.createDelegate(this, [node])
								}));
								menu.show(node.ui.getAnchor());
							}
						}
							
	});

	var ge = new Ext.tree.TreeEditor(checklist, { }, {
				        allowBlank:false,
				        blankText:'A name is required',
				        selectOnFocus:true
		    });

	checklist.addLayerFolder=function(node){
			node.select();
			var root = checklist.getRootNode();
			if(checklist.getSelectionModel().selNode){
					root = checklist.getSelectionModel().selNode;
			}
			
    		var node = root.appendChild(new Ext.tree.TreeNode({
        	        	text:'Dummy'
    	    	    ,	allowDrag:false
					,	leaf:false
					,	expanded:true
					,	checked:false
    			}));
			 
			checklist.getSelectionModel().select(node);
    		setTimeout(function(){
            	    ge.editNode = node;
	            	ge.startEdit(node.ui.textNode);
    			}, 10);
	};

	checklist.character_add_attribute = function(node){            
			node.select();
			function process(btn, text){
				if (btn === 'ok') {	
					if (text == '') {
						Ext.MessageBox.alert('Error','New value can not be blank.');
					} else {
						var conn = new Ext.data.Connection();
						conn.request({
									url:''
								,	method: 'POST'
								,	scope: this
								,	params: {value: text, nodeID: node.attributes.text}
								,	success: function(responseObject) {
											var o = Ext.decode(responseObject.responseText);
											node.appendChild(new Ext.tree.TreeNode({
													id : o.new_id,
													text : text,
													cls: 'file'
											}));
									}
								,	failure: function() {
										Ext.MessageBox.alert('Status', 'Unable to add layer.');
									}
							});
						}
					}
				};		

				Ext.MessageBox.prompt('New Layer','Enter name of Layer: ', process);
			};

	checklist.character_edit_attribute = function(node){            
					node.select();
				   	setTimeout(function(){
			                	    ge.editNode = node;
	        		            	ge.startEdit(node.ui.textNode);
	               }, 10);
		};
		
	checklist.character_delete_attribute = function(node){            

			function process(btn, text){
				if (btn == 'yes') {	
			
					}
				};		

			Ext.MessageBox.confirm('Delete Layer','All layer references will be removed from <b>' + node.text + '</b>.<br>Are you sure you wish to delete this layer?', process());
		}		
});
